package com.rmondjone.utils

import android.util.Log
import android.view.View
import com.rmondjone.androidcamerademo.R

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:防止短时间内多次点击
 */


fun View.setClickListener(click: ((e: View) -> Unit)) {


    setOnClickListener {

        val tag = it.getTag(R.id.click_time)
        val currentTimeMillis = System.currentTimeMillis()

        //判断 一下，防止类型出错http://baike.aiaibangzhu.com/uploads/20201130/867d193f3a7dd47a93065bc610f9059e.png
        if (tag is Long) {
            //1秒钟之内 不允许多次点击
            if (currentTimeMillis - tag < 1500) {
                Log.e("msg", "--------------- 拦截----- " + (currentTimeMillis - tag))
                return@setOnClickListener
            }
        }
        Log.e("msg", "--------------- 拦截 不拦截----- ")

        it.setTag(R.id.click_time, currentTimeMillis)
        click.invoke(it)


    }
}

fun View.filterRepeatClick(time: Int = 2000, click: ((e: View) -> Unit)) {
    val tag = this.getTag(R.id.click_time)
    val currentTimeMillis = System.currentTimeMillis()
    //判断 一下，防止类型出错，不能直接 as强转
    if (tag is Long) {
        //1秒钟之内 不允许多次点击
//        logcat("currentTimeMillis - tag---------- " + (currentTimeMillis - tag))
        if (currentTimeMillis - tag < time) {
            return
        }
    }
    this.setTag(R.id.click_time, currentTimeMillis)
    click.invoke(this)
}